import { useUserStore, useBuyerStore, useSellerStore } from "../store/user";

function isUser() {
  const userStore = useUserStore();
  return userStore.type === 12;
}

function hasBuyer() {
  const buyerStore = useBuyerStore();
  return buyerStore.id;
}

function hasSeller() {
  const sellerStore = useSellerStore();
  return sellerStore.id;
}

function isAudit() {
  const userStore = useUserStore();
  return userStore.type === 4;
}

function isAdmin() {
  const userStore = useUserStore();
  return userStore.type === 8;
}

function a() {
  const userStore = useUserStore(); // 交易审核人员
  return userStore.type === 5;
}

function b() {
  const userStore = useUserStore();
  return userStore.type === 3; // 资讯编辑人员
}
function c() {
  const userStore = useUserStore();
  return userStore.type === 7; // 资讯维护人员
}

function d() {
  const userStore = useUserStore();
  return userStore.type === 6; // 资讯审核人员
}

function hasCompany() {
  const userStore = useUserStore();
  return userStore.company?.companyName;
}

export const authConfig = {
  addUser: {
    auth() {
      return isAdmin();
    },
    redirect: "/",
  },
  showAddUser: {
    auth() {
      return isAdmin();
    },
    redirect: "/",
  },
  Enterprise: {
    auth() {
      return isUser();
    },
  },
  BuyerListing: {
    auth() {
      return isUser();
    },
  },
  SellerListing: {
    auth() {
      return isUser();
    },
  },
  OwnSeller: {
    auth() {
      return isUser();
    },
  },
  OwnBuyer: {
    auth() {
      return isUser();
    },
  },
  BuyerList: {
    auth() {
      return isUser() && hasSeller();
    },
    redirect: "/user/own/seller/listing/list",
  },
  SellerList: {
    auth() {
      return true;
    },
    redirect: "/user/own/buyer/listing/list",
  },
  UserAuditList: {
    auth() {
      return isAudit();
    },
  },
  UserAuditDetail: {
    auth() {
      return isAudit();
    },
  },
  BuyerListing: {
    auth() {
      return isUser() && hasCompany();
    },
    redirect: "/user/enterprise",
  },
  BuyerListing: {
    auth() {
      return isUser() && hasCompany();
    },
    redirect: "/user/enterprise",
  },
  OwnSeller: {
    auth() {
      return isUser() && hasCompany();
    },
    message: "请先注册企业信息，等待审核通过",
  },
  OwnBuyer: {
    auth() {
      return isUser() && hasCompany();
    },
    message: "请先注册企业信息，等待审核通过",
  },
  TransactionAuditList: {
    auth() {
      return a();
    },
  },
  TransactionAuditList: {
    auth() {
      return a();
    },
  },
  TransactionAuditDetail: {
    auth() {
      return a();
    },
  },
  AnnouncementAdd: {
    auth() {
      return b();
    },
  },
  AnnouncementVerify: {
    auth() {
      return c() || d();
    },
  },
  AnnouncementChange: {
    auth() {
      return c();
    },
  },
  AnnouncementVerifyDetail: {
    auth() {
      return d();
    },
  },
};
